// 审核新增加的信息,因为要把红娘的头像显示出来,所以需要使用云函数的lookup
//a_auditing,indexs的getnew用到
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
        // const wxContext = cloud.getWXContext()

        // return {
        //         event,
        //         openid: wxContext.OPENID,
        //         appid: wxContext.APPID,
        //         unionid: wxContext.UNIONID,
        // }
        console.log('event:', event)

        try {
                return await db.collection('user')
                        .aggregate()
                        .lookup({
                                from: 'admin',
                                localField: '_openid', //主表的条件._id
                                foreignField: '_openid', //从表的条件
                                as: 'TeacherList',
                        })
                        .match({
                                status: event.status
                        })
                        
                        //指定返回那些字段
                        .project({
                                _id: true,
                                access:true,
                                id:true,
                                image: true,
                                name: true,
                                sex: true,
                                age: true,
                                bodyheight:true,
                                bodyweight:true,
                                claim: true,
                                directions: true,
                                time: true,
                                marriage: true,
                                status: true, //把状态返回a_auditing需要依赖他显示是否待审核
                                'TeacherList.avatarUrl': true
                        })
                        //根据新字段排序
                        .sort({
                                [event.sortField]: -1, //从大到小
                        })
                        .skip(event.skip)
                        .limit(event.maxCount)
                        .end()

        } catch (err) {
                console.log(err)
                return err
        }



}